﻿using Aliyun.Acs.Core.Http;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// API基类
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/" + ApiVersion.Version)]
    public class BaseController<T> : ControllerBase where T : BaseController<T>
    {
        /// <summary>
        /// 日志
        /// </summary>
        protected readonly ILogger<T> Logger;
        protected readonly IRepository<ZmSystemLog> _systemLogRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="systemLogRepository"></param>
        public BaseController(ILogger<T> logger, IRepository<ZmSystemLog> systemLogRepository)
        {
            Logger = logger;
            _systemLogRepository = systemLogRepository;
        }

        protected void WriteSystemLog(string  userNumber, string actionName, RespRet respRet)
        {

                var dmSystemLog = new ZmSystemLog();
                dmSystemLog.Id = SequentialGuid.SequentialGuidGenerator.Instance.NewGuid().ToString("N");
                dmSystemLog.UserId ="admin";
                dmSystemLog.UserName = "admin";
                dmSystemLog.ActionName = actionName;
                dmSystemLog.CreateOn = DateTime.Now;
                dmSystemLog.ModifiedOn = DateTime.Now;
                dmSystemLog.Path = Request.Path;
                dmSystemLog.LoginType = Request.Headers["User-Agent"];
                dmSystemLog.LogTime = DateTime.Now;
                dmSystemLog.Ip = this.IpAddress();
                dmSystemLog.Respret = JsonConvert.SerializeObject(respRet);
                _systemLogRepository.Insert(dmSystemLog);
                _systemLogRepository.SaveChanges();
        }
    }
}
